package com.cainli.szbus.data.operation;

import java.util.ArrayList;

import android.os.Bundle;

import com.cainli.szbus.data.factory.BusStatsListJsonFactory;
import com.cainli.szbus.data.model.BusStat;
import com.cainli.szbus.data.requestmanager.RequestFactory;
import com.cainli.szbus.log.CLog;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.network.NetworkConnection.ConnectionResult;
import com.foxykeep.datadroid.requestmanager.Request;

/**
 * @author cainli
 *
 */
public class BusStatListOperation extends BaseOperation {
	private static final String TAG = BusStatListOperation.class.getSimpleName();
	public static final String PARAM_CITY = "city";
	public static final String PARAM_KEYWORD = "q";
	/* (non-Javadoc)
	 * @see com.cainli.szbus.data.operation.BaseOperation#getType()
	 */
	@Override
	protected String getType() {
		return "stats";
	}

	/* (non-Javadoc)
	 * @see com.cainli.szbus.data.operation.BaseOperation#getParams(com.foxykeep.datadroid.requestmanager.Request)
	 */
	@Override
	protected void initParams(Request request) {
		params.put(PARAM_CITY, request.getString(PARAM_CITY));
		params.put(PARAM_KEYWORD, request.getString(PARAM_KEYWORD));
	}

	/* (non-Javadoc)
	 * @see com.cainli.szbus.data.operation.BaseOperation#getParseResult(com.foxykeep.datadroid.network.NetworkConnection.ConnectionResult)
	 */
	@Override
	protected Bundle getParseResult(ConnectionResult result) {
		Bundle bundle = new Bundle();
		ArrayList<BusStat> busStatsList;
		try {
			busStatsList = BusStatsListJsonFactory
					.parseResult(result.body);
			bundle.putParcelableArrayList(RequestFactory.BUNDLE_EXTRA_BUSSTATS_LIST, busStatsList);
		} catch (DataException e) {
			CLog.d(TAG, e.getMessage());
		}
		return bundle;
	}
}
